package yqt.com.cnki.service.impl;

import haha.life.common.CutPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yqt.com.cnki.bo.DataBaseBo;
import yqt.com.cnki.dao.DIYTagDao;
import yqt.com.cnki.po.DataBasePo;
import yqt.com.cnki.po.SYS_CMS_OPTION_Po;
import yqt.com.cnki.po.SubmitFieldPo;
import yqt.com.cnki.service.DIYTagService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DIYTagServiceImpl implements DIYTagService {

    @Autowired
    DIYTagDao diyTagDao;

    @Override
    public List<Map<String, String>> getAllDBbase() {
        return diyTagDao.getAllDBbase();
    }

    @Override
    public CutPageBean<DataBaseBo> cutPage(DataBaseBo dataBaseBo) {
        CutPageBean<DataBaseBo> cutPageBean = new CutPageBean<>();
        List<DataBaseBo> dataBaseList  = diyTagDao.cutList(dataBaseBo);

        List<SYS_CMS_OPTION_Po> showToCustomer = diyTagDao.getAllShowToCustomer();

        for (DataBaseBo baseBo :dataBaseList) {
            Boolean updated = diyTagDao.isDataBaseUpdated(baseBo);
            if (updated){
                baseBo.setTemplateConig(true);
            }else {
                baseBo.setTemplateConig(false);
            }

            for (SYS_CMS_OPTION_Po sys_cms_option_po :showToCustomer) {
                if (baseBo.getDATABASECODE().equals(sys_cms_option_po.getDbCode())){
                    baseBo.setShowToCustomer(true);
                    break;
                }
            }

        }


        int count = diyTagDao.cutCount(dataBaseBo);
        cutPageBean.initCutPage(count, dataBaseBo.getPageSize(),dataBaseList);
        return cutPageBean;
    }

    @Override
    public List<SubmitFieldPo> getNotSysFieldsState(String DBcode) {
        List<SubmitFieldPo> res = diyTagDao.getNotSysFieldsState(DBcode);
        List<SubmitFieldPo> res2 = diyTagDao.getSubmitFiled(DBcode);

        for (SubmitFieldPo poInTotal :res) {
            String filedInTotal = poInTotal.getField();
            for (SubmitFieldPo poInSubmit :res2) {
                String filedInSubmit = poInSubmit.getFieldName();
                if (filedInTotal.equals(filedInSubmit)){
                    poInTotal.setIsPublish(true);
                    poInTotal.setFieldDisplaydName(poInSubmit.getFieldDisplaydName());
                    poInTotal.setFieldType(poInSubmit.getFieldType());
                    poInTotal.setDefaultValue(poInSubmit.getDefaultValue());
                    poInTotal.setIsAllowNull(poInSubmit.getIsAllowNull());
                    poInTotal.setIsOnlyOne(poInSubmit.getIsOnlyOne());
                    poInTotal.setSortNum(poInSubmit.getSortNum());
                    poInTotal.setDescript(poInSubmit.getDescript());
                    break;
                }
            }

        }


        return res;
    }

    @Override
    public Boolean showToCustomerChange(DataBasePo dataBasePo) {
        return diyTagDao.showToCustomerChange(dataBasePo);
    }

    @Override
    public Boolean save(String dbCode,ArrayList<SubmitFieldPo> params) {
        return diyTagDao.save(dbCode,params);
    }
}
